import axios from "axios";
import webSocketWithServer from "@/utils/websocket";
import Vue from "vue";

export default {
    namespaced: true,
    state: {
        serverList: [
            {
                ip: 'legendmail.online',
                port: '',
                protocol: 'http',
                sendQueue: [],
                enable: true
            }, {
                ip: 'magicbyte.top',
                port: '',
                protocol: 'https',
                sendQueue: [],
                enable: true
            },
            {
                ip: 'legendmail.website',
                port: '',
                protocol: 'http',
                sendQueue: [],
                enable: true
            }, {
                ip: 'legendmail.press',
                port: '',
                protocol: 'http',
                sendQueue: [],
                enable: true
            }, {
                ip: 'legendmail.fun',
                port: '',
                protocol: 'http',
                sendQueue: [],
                enable: true
            }, {
                ip: 'legendmail.asia',
                port: '',
                protocol: 'http',
                sendQueue: [],
                enable: true
            },

        ],
        currentServer: {}
    }, getters: {
        getCurrentServer(state) {
            let item = sessionStorage.getItem("serverIndex");
            if (item) {
                Vue.set(state, 'currentServer', state.serverList[parseInt(item)]);
                axios.defaults.baseURL = `${state.currentServer.protocol}://${state.currentServer.ip}${state.currentServer.port ? ':' + state.currentServer.port : ''}/${state.currentServer.ip === 'localhost' ? '' : 'email-tool-back'}/`;
                return state.currentServer;
            } else {
                sessionStorage.setItem("serverIndex", "0");
                Vue.set(state, 'currentServer', state.serverList[0]);
                axios.defaults.baseURL = `${state.currentServer.protocol}://${state.currentServer.ip}${state.currentServer.port ? ':' + state.currentServer.port : ''}/${state.currentServer.ip === 'localhost' ? '' : 'email-tool-back'}/`;
                return state.currentServer;
            }
        },
        getServers(state) {
            return state.serverList;
        }
    }, mutations: {
        //更改服务器代理对象
        changeServer(state, index) {
            sessionStorage.setItem("serverIndex", index.toString());
            let currentItem = state.serverList[index];
            axios.defaults.baseURL = `${currentItem.protocol}://${currentItem.ip}${currentItem.port ? ':' + currentItem.port : ''}/${currentItem.ip === 'localhost' ? '' : 'email-tool-back'}/`;
            //响应性赋值
            Vue.set(state, 'currentServer', currentItem);
        }
    }, actions: {
        getServerState({state}) {
            for (let i = 0; i < state.serverList.length; i++) {
                let item = state.serverList[i];
                webSocketWithServer(item, `${item.protocol === "http" ? "ws" : "wss"}://${item.ip}${item.port ? ':' : ''}${item.port}/${item.ip === 'localhost' ? '' : 'email-tool-back'}/webSocketIMServer`, 'sendQueue');
            }
        }
    }
}